﻿#include "IPLCalculationUIComponents.h"
#include "ui_IPLCalculationUIComponents.h"
#include <QModelIndexList>
#include <QDoubleValidator>
#include "DataChosenListDialog.h"
#include "IdaWidgetHelper.h"

namespace pst
{
    IPLCalculationUIComponents::IPLCalculationUIComponents(QWidget* parent)
        : QWidget(parent), ui(new Ui::IPLCalculationUIComponents()), m_dataChosenListDialog(nullptr)
    {
        ui->setupUi(this);
        ui->pushButton_Curve->setIcon(QIcon(":/rc/apostrophe.png"));
        ui->pushButton_Curve->setStyleSheet("QPushButton { text-align: center; }");

        QDoubleValidator* doubleValidator = new QDoubleValidator(0, 999999999.9999, 3, this);
        doubleValidator->setNotation(QDoubleValidator::StandardNotation);
        ui->lineEdit_value->setValidator(doubleValidator);
        on_radioButton_value_toggled(true);
    }

    IPLCalculationUIComponents::~IPLCalculationUIComponents()
    {
        delete ui;
    }
    void IPLCalculationUIComponents::setUIComponentsName(const QString& namePartChinese, const QString& namePartUppercase, const QString& namePartLowercase, const QString& namePartUnit)
    {
        m_nameAbbreviationLetter = QString("%1<sub>%2</sub>")
                                       .arg(namePartUppercase)
                                       .arg(namePartLowercase);
        QString finalStr = QString("%1%2%3")
                               .arg(namePartChinese)
                               .arg(m_nameAbbreviationLetter)
                               .arg(namePartUnit);
        ui->label_name->setText(finalStr);
        ui->label_name->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    }

    void IPLCalculationUIComponents::setInit(const QString& param)
    {
        bool ok;
        param.toInt(&ok);
        if (ok)
        {
            on_radioButton_value_toggled(true);
            ui->lineEdit_value->setText(param);
        }
        else
        {
            on_radioButton_value_toggled(false);
            slot_sentChosenDataIds(createSelectedIndexes(QStringList() << param));
        }
    }

    QString IPLCalculationUIComponents::getUIComponentsNameAbbreviationLetter()
    {
        return m_nameAbbreviationLetter;
    }

    void IPLCalculationUIComponents::setCurveNameList(const QList<QString>& curveNameList)
    {
        m_curveNameList = curveNameList;
        m_model.setStringList(m_curveNameList);
    }

    void IPLCalculationUIComponents::on_radioButton_value_toggled(bool checked)
    {
        ui->radioButton_value->setChecked(checked);
        ui->lineEdit_value->setEnabled(checked);
        ui->radioButton_curve->setChecked(!checked);
        ui->pushButton_Curve->setEnabled(!checked);
    }

    void IPLCalculationUIComponents::on_pushButton_Curve_clicked()
    {
        m_dataChosenListDialog = new DataChosenListDialog();
        m_dataChosenListDialog->setFixedSize(300, 280);
        // m_dataChosenListDialog->moveToCenter();
        m_dataChosenListDialog->setMultipleSelected(false);
        m_dataChosenListDialog->setData(m_curveNameList);
        m_dataChosenListDialog->updateUI();
        m_dataChosenListDialog->showWindow();
        m_dataChosenListDialog->setSelctedIndexs(m_selectedIndexs); // 默认选中第一个

        connect(m_dataChosenListDialog,
                &DataChosenListDialog::signal_sentChosenDataIds,
                this, &IPLCalculationUIComponents::slot_sentChosenDataIds);
    }

    void IPLCalculationUIComponents::slot_sentChosenDataIds(const QModelIndexList& chosenID)
    {
        m_selectedIndexs.clear();
        auto count = chosenID.count();
        QString strText{};
        for (int i = 0; i < count; ++i)
        {
            auto chosenData = chosenID.at(i);
            if (chosenData.isValid())
            {
                auto str = chosenData.data().toString();
                strText += str + ",";

                auto index = chosenID.at(i).row();
                m_selectedIndexs.append(index);
            }
        }
        if (!m_selectedIndexs.isEmpty())
            IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_Curve, true, Qt::blue);
        else
            IdaWidgetHelperInstance->setPushButtonSelcetBarCheckedState(ui->pushButton_Curve, false, Qt::white);
    }

    bool IPLCalculationUIComponents::getRadioButtonValueState()
    {
        return ui->radioButton_value->isChecked();
    }

    double IPLCalculationUIComponents::getIPLValue()
    {
        double _value = -1;
        if (!ui->lineEdit_value->text().isEmpty())
            _value = ui->lineEdit_value->text().toDouble();
        return _value;
    }

    QList<int> IPLCalculationUIComponents::getIPLSeletedCurveIndexs()
    {
        return m_selectedIndexs;
    }

    QModelIndexList IPLCalculationUIComponents::createSelectedIndexes(const QStringList& params) {
        QModelIndexList selectedIndexes;

        foreach(const QString & param, params) {
            int row = m_curveNameList.indexOf(param);
            if (row != -1) {
                // 使用成员模型m_model创建索引，确保索引与模型生命周期一致
                QModelIndex index = m_model.index(row, 0);
                if (index.isValid()) {
                    selectedIndexes.append(index);
                }
            }
        }
        return selectedIndexes;
    }

} // namespace pst
